import Vue from 'vue'
import { HandleMessage } from './types'
import Message from './Message'

let $message: Message

// 初始化
const init = function() {
  const MessageContainerId = '_message_container_'
  const MessageContainer = document.createElement('div')
  MessageContainer.setAttribute('id', MessageContainerId)

  document.body.appendChild(MessageContainer)

  setTimeout(function() {
    $message = new Message().$mount(`#${MessageContainerId}`)
  }, 0)
}

// 弹出消息
const message: HandleMessage = function(message = '') {
  if ($message) {
    $message.message = message

    $message.$nextTick(function() {
      $message.show()
    })
  }
}

export default {
  install: function(vue: typeof Vue) {
    init()
    vue.prototype.$message = message
  },
}

export {
  message,
}

